package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hermit.qmst.R;
import com.hermit.qmst.UI.activity.BusinessActivity;
import com.hermit.qmst.UI.activity.OpenUrlActivity;
import com.hermit.qmst.UI.activity.PayCardActivity;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by Administrator on 2017/1/4 0004.
 */
@ContentView(R.layout.new_activity_find)
public class FindActivity extends Activity implements View.OnClickListener {
    @ViewInject(R.id.iv_back)
    ImageView mIvBack;
    @ViewInject(R.id.tv_title)
    TextView mTvTitle;
    @ViewInject(R.id.tv_item_1)
    TextView mTvItem1;
    @ViewInject(R.id.tv_item_2)
    TextView mTvItem2;
    @ViewInject(R.id.tv_item_3)
    TextView mTvItem3;
    @ViewInject(R.id.tv_item_4)
    TextView mTvItem4;
    @ViewInject(R.id.tv_item_5)
    TextView mTvItem5;
    @ViewInject(R.id.tv_item_6)
    TextView mTvItem6;
    @ViewInject(R.id.tv_item_7)
    TextView mTvItem7;
    private String TAG = "FindActivity";
    private String number = "";
    private String loadUrl = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mIvBack.setVisibility(View.VISIBLE);
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTvTitle.setText("大本营");
        mTvItem1.setOnClickListener(this);
        mTvItem2.setOnClickListener(this);
        mTvItem3.setOnClickListener(this);
        mTvItem4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (number.length() > 0) {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
                    startActivity(intent);
                } else {
                    Toast.makeText(FindActivity.this, "暂无咨询热线号码", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mTvItem5.setOnClickListener(this);
        mTvItem6.setOnClickListener(this);
        mTvItem7.setOnClickListener(this);
        getPhone();
        getLoadUrl();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.tv_item_1:
                intent.setClass(FindActivity.this, ContactActivity.class);
                break;
            case R.id.tv_item_2:
                intent.putExtra("title", "企业链接");
                intent.putExtra("url", loadUrl);
                intent.setClass(FindActivity.this, OpenUrlActivity.class);
                break;
            case R.id.tv_item_3:
                intent.putExtra("title", mTvItem3.getText().toString());
                intent.setClass(FindActivity.this, BusinessActivity.class);
                break;
            case R.id.tv_item_5:
                intent.setClass(FindActivity.this, MessageActivity.class);
                break;
            case R.id.tv_item_6:
                intent.setClass(FindActivity.this, PayCardActivity.class);
                break;
            case R.id.tv_item_7:
                intent.setClass(FindActivity.this, SetActivity.class);
                break;
        }
        startActivity(intent);
    }

    private void getPhone() {
        RequestQueue requestQueue = Volley.newRequestQueue(FindActivity.this);
        String url = Common.mUrl + "szdh/discovery/consultTel.php?agentId=" + Common.mAgentId;
        Log.i(TAG + "getPhone_url", url, true);

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                Log.i(TAG + "getPhone_request", object.toString(), true);
                try {
                    number = object.getString("phone");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        });
        requestQueue.add(request);
        Log.i(TAG, url, true);
    }

    private void getLoadUrl() {
        RequestQueue requestQueue = Volley.newRequestQueue(FindActivity.this);
        String url = Common.mUrl + "szdh/discovery/officialUrl.php?agenId=" + Common.mAgentId;
        Log.i(TAG + "getLoadUrl_url", url, true);

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                Log.i(TAG + "getLoadUrl_request", object.toString(), true);
                try {
                    if (object.getBoolean("state")) {
                        loadUrl = object.getString("url");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        });
        requestQueue.add(request);
        Log.i(TAG, url, true);
    }
}
